//
//  ScHeaderView.m
//  Camera
//
//  Created by gml on 2018/10/31.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "GMLHomeHeaderView.h"
#import "GMLHomeItemView.h"

#define kNaviH  (SAFE_SCREEN_BOTTOM>0.0 ? (SAFE_SCREEN_TOP+44) : 64)
@interface GMLHomeHeaderView()
@property (nonatomic,strong) CAGradientLayer *gradientLayer2;
@end

@implementation GMLHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = self.bounds;
        gradientLayer.colors   = @[(__bridge NSArray * _Nullable)(UIColorFromRGB(0x474747).CGColor), (__bridge NSArray * _Nullable)(UIColorFromRGB(0x1E1E1E).CGColor)];;
        // 颜色分割线位置( 默认 0，1 )
        gradientLayer.locations    = @[@(0),@(1)];
        // 起始点 坐标是 左上 （0，0）  右下 （1，1）
        gradientLayer.startPoint  = CGPointZero;
        // 结束点
        gradientLayer.endPoint = CGPointMake(1, 0);
        [self.layer addSublayer:gradientLayer];
        
        // 1 背景色
        CAGradientLayer *gradientLayer2 = [CAGradientLayer layer];
        gradientLayer2.frame = self.bounds;
        //  [UIColor colorWithRed:52.0 / 255.0 green:104.0 / 255.0 blue:206.0 / 255.0 alpha:1]
        // 105,105,105
        gradientLayer2.colors   = @[(__bridge NSArray * _Nullable)(UIColorFromRGB(0x474747).CGColor), (__bridge NSArray * _Nullable)([UIColor clearColor].CGColor)];;
        // 颜色分割线位置( 默认 0，1 )
        gradientLayer2.locations    = @[@(0),@(1)];
        // 起始点 坐标是 左上 （0，0）  右下 （1，1）
        gradientLayer2.startPoint  = CGPointMake(0, 1);
        // 结束点
        gradientLayer2.endPoint = CGPointMake(0, 0.5);
        gradientLayer2.opacity = 0.1;
        [self.layer addSublayer:gradientLayer2];
        self.gradientLayer2 = gradientLayer2;
        
        UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:contentView];
        
        CGFloat x = 0;
        CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
        // 2 子控件
        for (int index = 0; index < 3; index++) {
            GMLHomeItemView *itemview = [GMLHomeItemView loadItemView];
            itemview.frame = CGRectMake(x, frame.origin.y+kNaviH, screenWidth / 3, kHeaderViewH-kNaviH);
//            [self addSubview:itemview];
            x = x + screenWidth / 3;
            [contentView addSubview:itemview];
        }
    }
    
    return self;
}

#pragma mark - Set方法
-(void)setContentOffsetY:(CGFloat)contentOffsetY {
    _contentOffsetY = contentOffsetY;
    
    CGFloat alpha = ( kNaviH-contentOffsetY ) / kNaviH  ;
    
    CGFloat opacity = 1-alpha+0.1;
    if (opacity > 0.3) {
        opacity = 0.3;
    }
    self.gradientLayer2.opacity = opacity;
    
    if (alpha<0.5) {
        alpha = 0.5;
    }
    CGFloat scale = alpha * 2;
    if (scale > 1.0) {
        scale = 1.0;
    }else if (scale < 0.55){
        scale = 0.55;
    }
    NSLog(@"alpha = %f",alpha);
    for (UIView *subview in self.subviews) {
        subview.alpha = alpha;
        CGAffineTransform transform = CGAffineTransformMakeScale(alpha, alpha);
        subview.transform = CGAffineTransformTranslate(transform, 0, contentOffsetY*1.0);
    }
    
    
}


@end
